package org.jeecgframework.web.service.impl;
import org.apache.poi.ss.usermodel.*;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.CbsWarehouseServiceI;
import org.jeecgframework.web.system.pojo.base.CbsWarehouseEntity;
import org.jeecgframework.web.system.pojo.base.CbsWarehouseSlaEntity;
import org.jeecgframework.web.system.pojo.base.TSTerritory;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.UUID;
import java.io.Serializable;

@Service("cbsWarehouseService")
@Transactional
public class CbsWarehouseServiceImpl extends CommonServiceImpl implements CbsWarehouseServiceI {
	private Workbook workbook;
	private Sheet sheet;
	private Row row;
	@Autowired
	private SystemService systemService;
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((CbsWarehouseEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((CbsWarehouseEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((CbsWarehouseEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(CbsWarehouseEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(CbsWarehouseEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(CbsWarehouseEntity t){
	 	return true;
 	}

	@Override
	public void readFileToMsg(String filePath) throws Exception {

		readFile(filePath);
		readLine();
	}
	public void readFile(String filePath) throws Exception {
		try {
			File excelFile = new File(filePath);
			FileInputStream is = new FileInputStream(excelFile);
			workbook = WorkbookFactory.create(is);
			sheet = workbook.getSheetAt(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 *
	 * @Title: readLine
	 * @Description: 读取每一行的信息
	 * @return void null
	 */
	public void readLine() {
		Iterator<Row> rows = sheet.rowIterator();
		while (rows.hasNext()) {
			row = (Row) rows.next();
			if (row.getRowNum() ==0 ) {
				continue;
			}
            String ckName = null;
            String city = null;
            String slaType = null;
            String isPri = null;

			Iterator<Cell> cells = row.cellIterator();

			while (cells.hasNext()) {

                Cell cell = (Cell) cells.next();

                if (cell.getColumnIndex() == 1) {
					ckName = cell.getStringCellValue();
					System.out.println(ckName);
					continue;
				}
				if (cell.getColumnIndex() == 3) {
					city = cell.getStringCellValue();
					System.out.println(city);
					continue;
				}
				if (cell.getColumnIndex() == 4) {

					slaType = cell.getStringCellValue();
					System.out.println(slaType);
					continue;
				}
				if (cell.getColumnIndex() == 5) {

					isPri = cell.getStringCellValue();
					System.out.println(isPri);
					continue;
				}
			}
            	CbsWarehouseEntity cbsWarehouseEntity =systemService.singleResult("from CbsWarehouseEntity where name = '"+ckName+"'");
                TSTerritory tsTerritory = systemService.singleResult("from TSTerritory where territoryName = '"+city+"' and territoryLevel = '2'");
                if (StringUtil.isNotEmpty(tsTerritory)){
                    CbsWarehouseSlaEntity cbsWarehouseSlaEntity = new CbsWarehouseSlaEntity();
                    cbsWarehouseSlaEntity.setCbsWarehouseId(cbsWarehouseEntity);

                    cbsWarehouseSlaEntity.setShi(tsTerritory.getId());
                    if ("SL1".equals(slaType)){
                        cbsWarehouseSlaEntity.setSlaType("1");
                    }else if ("SL2".equals(slaType)){
                        cbsWarehouseSlaEntity.setSlaType("2");
                    }else if ("SL3".equals(slaType)){
                        cbsWarehouseSlaEntity.setSlaType("3");
                    }else if ("SL4".equals(slaType)){
                        cbsWarehouseSlaEntity.setSlaType("4");
                    }

                    if ("Y".equals(isPri)){
                        cbsWarehouseSlaEntity.setIsPriority("1");
                    }
                    if ("N".equals(isPri)){
                        cbsWarehouseSlaEntity.setIsPriority("0");
                    }
                    cbsWarehouseSlaEntity.setCreateName("管理员");
                    cbsWarehouseSlaEntity.setCreateDate(new Date());
                    cbsWarehouseSlaEntity.setCreateBy("admin");
                    systemService.saveOrUpdate(cbsWarehouseSlaEntity);
                }else {
                	System.err.println(ckName+"----"+city+"---"+slaType+"---"+isPri);
				}

		}
	}

	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,CbsWarehouseEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{name}",String.valueOf(t.getName()));
 		sql  = sql.replace("#{code}",String.valueOf(t.getCode()));
 		sql  = sql.replace("#{logistics}",String.valueOf(t.getLogistics()));
 		sql  = sql.replace("#{sheng}",String.valueOf(t.getSheng()));
 		sql  = sql.replace("#{shi}",String.valueOf(t.getShi()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}